using System;
using System.Runtime.InteropServices;
using Microsoft.SharePoint;
using Microsoft.SharePoint.UserCode;
using Microsoft.SharePoint.Administration;
using Microsoft.Office.Server.Diagnostics;
using SharePointLoggerArgs;

namespace SharePointLogger
{
    [Guid("729a1c8e-66ad-43c4-b421-9bf3dd78c0a2")]
    public class MainEventReceiver : SPFeatureReceiver
    {
        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            try
            {
                SPUserCodeService userCodeService = SPUserCodeService.Local;

                if (userCodeService != null)
                {

                    SPProxyOperationType traceOperation =
                        new SPProxyOperationType(
                            new TraceProxyArgs().TraceProxyOpsAssemblyName,
                            new TraceProxyArgs().TraceProxyOpsTypeName);

                    userCodeService.ProxyOperationTypes.Add(traceOperation);
                    userCodeService.Update();

                    PortalLog.LogString("SharePoint Logger: {0}", "Successfully registered SharePointLogger");
                }
                else
                {
                    PortalLog.LogString("SharePoint Logger: {0}", "UserCode Service not available");
                }
            }
            catch (Exception X)
            {
                PortalLog.LogString("SharePoint Logger: {0}", X.Message);
            }
        }

        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            try
            {
                SPUserCodeService userCodeService = SPUserCodeService.Local;

                if (userCodeService != null)
                {
                    SPProxyOperationType traceOperation = null; ;
                    foreach (SPProxyOperationType operationType in userCodeService.ProxyOperationTypes)
                    {
                        if (operationType.AssemblyName.Equals(new TraceProxyArgs().TraceProxyOpsAssemblyName, StringComparison.CurrentCultureIgnoreCase))
                        {
                            traceOperation = operationType;
                            break;
                        }
                    }

                    if (traceOperation != null)
                    {
                        userCodeService.ProxyOperationTypes.Remove(traceOperation);
                        userCodeService.Update();
                    }

                    PortalLog.LogString("SharePoint Logger: {0}", "Successfully unregistered SharePointLogger");

                }
                else
                {
                    PortalLog.LogString("SharePoint Logger: {0}", "UserCode Service not available");
                }
            }
            catch (Exception X)
            {
                PortalLog.LogString("SharePoint Logger: {0}", X.Message);
            }
        }
    }
}
